<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../../vue.js"></script>
  <script src="../../vue-router.js"></script>

  <style>
    .active {
      font-weight: 500;
      color: red;
    }
  </style>
</head>

<body>

  <div id="app">

    <ul>
      <router-link tag="li" to="/" exact active-class="active">首页</router-link>
      <router-link tag="li" to="/position" active-class="active">职位</router-link>
      <router-link tag="li" to="/search/fe" active-class="active">搜索fe</router-link>
      <router-link tag="li" to="/search/be" active-class="active">搜索be</router-link>
      <router-link tag="li" to="/login" active-class="active">登录</router-link>
    </ul>
    <router-view></router-view>
  </div>

  <script>
    const position = {
      template: `<div>postion</div>`,
      beforeRouteEnter(to, from, next) {
        next();
      }
    }

    const search = {
      template: `<div>search</div>`,
      beforeRouteUpdate(to, from, next) {
        console.log('beforeRouteUpdate:', to)
      },
      beforeRouteLeave(to, from, next) {
        let rs = confirm("确认要离开么？")
        if (rs) {
          next()
        }
      }
    }



    var router = new VueRouter({
      // mode: 'history',
      routes: [
        {
          path: "/login",
          component: {
            template: `<div>login page</div>`
          }
        },
        {
          path: '/position',
          component: position,
          meta: {
            auth: true
          }
        },
        {
          path: '/search/:keyword',
          component: search
        }
      ]
    })

    var islogin = false;
    router.beforeEach((to, from, next) => {
      console.log('beforeEach', to, from)
      // //....
      //next(false);中断当前的导航
      if (!islogin && to.meta.auth) {
        islogin = true;
        next({
          path: '/login'
        });
      } else {
        next();
      }
    })

    // router.beforeResolve((to, from, next) => {
    //   console.log('beforeResolve')
    // })


    // router.afterEach((to, from) => {
    //   console.log('afterEach')
    // })

    var vm = new Vue({
      router,
      el: '#app'
    })

  </script>

</body>

</html>